package com.gitee.secretopen.sso.controller;


import cn.chenc.framework.core.annotation.BussinessLog;
import cn.chenc.framework.core.model.request.RequestHolder;
import cn.chenc.framework.core.model.response.ResponseVO;
import cn.chenc.framework.core.model.response.ResultUtil;
import cn.chenc.framework.core.util.Md5Util;
import cn.chenc.framework.excel.util.EasyExcelUtil;
import com.gitee.secretopen.sso.controller.form.PageInfo;
import com.gitee.secretopen.sso.db.pojo.User;
import com.gitee.secretopen.sso.excelmodel.UserUpload;
import com.gitee.secretopen.sso.service.UserService;
import com.google.common.primitives.Ints;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Random;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author secret
 * @since 2021-02-25
 */
@RestController
@RequestMapping("/admin")
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping("/user/{id}")
    public ResponseVO<User> getOne(@PathVariable("id") int id){
        User user = userService.getById(id);
        return ResultUtil.success(user);
    }

    @PostMapping("/users")
    @BussinessLog("查询用户列表")
    public ResponseVO<PageInfo<User>> list(@RequestBody User params){
        PageInfo<User> pages =userService.page(params);
        return ResultUtil.success(pages);
    }

    @PostMapping("/user/getByName")
    public ResponseVO<PageInfo<User>> getByName(@RequestBody User user) {
        PageInfo<User> pages = userService.getByName(user);
        return ResultUtil.success(pages);
    }

    @GetMapping("/user/getByToken")
    public ResponseVO<PageInfo<User>> getLoginUser() {
        User userInfo = userService.getLoginUser();
        return ResultUtil.success(userInfo);
    }

    @PostMapping("/user")
    @BussinessLog("添加用户")
    public ResponseVO save(@RequestBody User user){
        userService.addUser(user);
        return ResultUtil.success("新增成功！");
    }

    @PutMapping("/user/resetPwd")
    public ResponseVO resetPwd(@RequestBody User user){
        user.setPassword(Md5Util.MD5(user.getPassword(),user.getName()));
        user.setUpdateTime(LocalDateTime.now());
        userService.updateById(user);
        return ResultUtil.success("密码修改成功！");
    }

    @PutMapping("/user")
    public ResponseVO update(@RequestBody User user) {
        User user1 = userService.getById(user.getId());
        if (user1 == null) {
            return ResultUtil.error("更新失败！");
        }
        user1.setIsLoginSsoAdmin(user.getIsLoginSsoAdmin());
        user1.setUpdateTime(LocalDateTime.now());
        userService.updateById(user1);
        return ResultUtil.success("更新成功！");
    }

    @DeleteMapping("/user/{id}")
    public ResponseVO delete(@PathVariable("id") int id) {
        userService.removeById(id);
        return ResultUtil.success("删除成功");
    }

    @DeleteMapping("/users")
    public ResponseVO deleteMulti(@RequestBody int[] ids) {
        List<Integer> id = Ints.asList(ids);
        userService.removeByIds(id);
        return ResultUtil.success("删除成功");
    }

    @GetMapping("/userAdd")
    public ResponseVO insert() {
        User user = new User();
        user.setName("test"+new Random().nextInt(100));
        user.setPassword(Md5Util.MD5("123456",user.getName()));
        user.setCreateTime(LocalDateTime.now());
        user.setCreateBy("root");
        user.setUpdateTime(LocalDateTime.now());
        user.setUpdateBy("root");
        user.setIsLoginSsoAdmin(1);
        user.setIsDelete(0);
        boolean result = userService.save(user);
        if (result){
            return ResultUtil.success("新增用户成功！");
        }
        return ResultUtil.error("失败！");
    }

    @ResponseBody
    @PostMapping(value = "/users/download")
    @BussinessLog("用户列表导出")
    public void downloadUsers(){
        List<User> list=userService.downloadUsers();
        HttpServletResponse response = RequestHolder.getResponse();
        EasyExcelUtil.toExcle(response, list, "secret_sso_用户清单", UserUpload.class);
    }

    @ResponseBody
    @PostMapping(value = "/users/upload")
    @BussinessLog("批量导入用户")
    public ResponseVO upload(MultipartFile file) throws Exception {
        ResponseVO result = userService.upload(file);
        return result;
    }
}

